package zheezes.protocol.impl.gbw;

import zheezes.util.ByteUtil;

public class GbwConAddr {
	public static final int SIZE = 4;
	
	private byte[] addr = new byte[SIZE];
	
	public GbwConAddr() {
	}
	
	public GbwConAddr(int addr) {
		this(ByteUtil.bltoc(addr));
	}

	public GbwConAddr(String addr) {
		this(ByteUtil.stoa(addr.toCharArray()));
	}
	
	public GbwConAddr(byte[] addr) {
		System.arraycopy(addr, 0, this.addr, 0, this.addr.length);
	}
	
	public int sizeof() {
		return SIZE;
	}
	
	public int setBytes(byte[] buf, int offset) {
		addr[0] = buf[offset + 1];
		addr[1] = buf[offset + 0];
		addr[2] = buf[offset + 3];
		addr[3] = buf[offset + 2];

		return 4;
	}
	
	public byte[] getBytes() {
		byte[] buf = new byte[sizeof()];
		getBytes(buf, 0);
		return buf;
	}
	
	public int getBytes(byte[] buf, int offset) {
		int idx = offset;
		buf[idx++] = addr[1];
		buf[idx++] = addr[0];
		buf[idx++] = addr[3];
		buf[idx++] = addr[2];
		
		return idx - offset;
	}
}
